package com.plum.admin.config.security;

import com.plum.admin.common.constant.Constant;
import com.plum.admin.modules.sys.entity.SysUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * TODO
 *
 * @author 子丶沫
 * @version 1.0
 * @date 2021/8/13 11:47
 */
@Data
public class SecurityUserDetails implements UserDetails {
    private String userId;
    private String username;
    private String password;
    private Set<String> permissions;
    private Integer lockFlag;
    private SysUser sysUser;
    public SecurityUserDetails(String userId, String username, String password, Set<String> permissions, Integer lockFlag) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.permissions = permissions;
        this.lockFlag = lockFlag;
    }
    public SecurityUserDetails(SysUser user){
        this.sysUser=user;
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorityList=new ArrayList<>();
        for (String permission : permissions) {
            authorityList.add(new SimpleGrantedAuthority(permission));
        }
        return authorityList;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !Constant.USER_STATUS_LOCK.equals(this.lockFlag);
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return Constant.USER_STATUS_NORMAL.equals(this.lockFlag);
    }
}
